<?php

use common\models\business\BusinessContractCategory;
use yii\helpers\Html;
use kartik\grid\GridView;
use yii\helpers\ArrayHelper;
use common\components\Helper;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\business\models\BusinessContractSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '合同';
$this->params['breadcrumbs'][] = $this->title;
?>
<style media=print type="text/css">
    .noprint{visibility:hidden}
</style>

<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
              <?= Html::a('返回','javascript:history.back(-1)'); ?>  <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">
 


<div class="business-contract-index">

    <?php
    try {
        echo GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'showPageSummary' => true,
            'pjax' => true,
            'striped' => true,
            'hover' => true,
            'panel' => ['type' => 'primary', 'heading' => '合同往来汇总'],
            'containerOptions' => ['style' => 'overflow: auto'], // only set when $responsive = false
            'headerRowOptions' => ['class' => 'kartik-sheet-style'],
            'filterRowOptions' => ['class' => 'kartik-sheet-style'],
            'columns' => [

                [
                    'class' => 'kartik\grid\ExpandRowColumn',
                    'width' => '50px',
                    'value' => function ($model, $key, $index, $column) {
                        return GridView::ROW_COLLAPSED;
                    },
                    'detail' => function ($model, $key, $index, $column) {
                        return Yii::$app->controller->renderPartial('_detail', ['model' => $model]);
                    },
                    'headerOptions' => ['class' => 'kartik-sheet-style'],
                    'expandOneOnly' => true
                ],

                [
                    'attribute' => 'no',
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                    'width' => '100px',
                ],
                [
                    'attribute' => 'name',
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                    'width' => '100px',
                ],
                [
                    'label' => '已购进',
                    'format' => 'raw',
                    'value' => function ($model) {
                        return $model->getFinishNum() . '头';
                    },
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                    'width' => '100px',
                ],
                [
                    'label' => '未购进',
                    'format' => 'raw',
                    'value' => function ($model) {
                        $showpre = BusinessContractCategory::showPre(BusinessContractCategory::findOne($model->cateid));
                        if (!empty($showpre) && array_key_exists('amount',array_flip($showpre))) {
                            return ($model->num - $model->getFinishNum()) . '头';
                        }else{
                            return "/";
                        }
                    },
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                    'width' => '100px',
                ],
                [
                    'label' => '已结算',
                    'format' => 'raw',
                    'value' => function ($model) {
                        return $model->getFinishPrice() . '元';
                    },
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                    'width' => '100px',
                ],
                [
                    'label' => '未结算',
                    'format' => 'raw',
                    'value' => function ($model) {
                        $showpre = BusinessContractCategory::showPre(BusinessContractCategory::findOne($model->cateid));
                        if (!empty($showpre) && array_key_exists('num',array_flip($showpre))) {
                            return ($model->amount - $model->getFinishPrice()) . '元';
                        }else{
                            return '/';
                        }
                    },
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                    'width' => '100px',
                ],
                [
                    'attribute' => 'start_time',
                    'value' => function ($model) {
                        return date('Y-m-d', $model->start_time);

                    },
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                    'width' => '100px',
                ],
                [
                    'attribute' => 'end_time',
                    'value' => function ($model) {
                        return date('Y-m-d', $model->end_time);

                    },
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                    'width' => '100px',
                ],
                [
                    'label' => '状态',
                    'format' => 'raw',
                    'attribute' => 'status',
                    'width' => '170px',
                    'value' => function ($model) {
                        $setouttime = \common\models\sys\SysSetting::getValue('contract_out_time');
                        $outdate = $setouttime * 3600 * 24 + time();
                        if ($model->end_time < time()) {
                            return Html::label('已过期', '', ['class' => 'btn btn-info btn-xs']);
                        } else if ($model->end_time < $outdate) {
                            return Html::Label('剩余' . Helper::dateDifference(date('Y-m-d', $model->end_time), date('Y-m-d')) . '天过期', '', ['class' => 'btn btn-danger btn-xs']);
                        } else {
//                          return '距离过期还有'.Helper::dateDifference(date('Y-m-d',$model->end_time),date('Y-m-d')).'天';
                            return Html::label('生效中', '', ['class' => 'btn btn-success btn-xs']);
                        }
                    },
                    'filterType' => GridView::FILTER_SELECT2,
                    'filter' => \common\models\business\BusinessContract::$statues,
                    'filterWidgetOptions' => [
                        'pluginOptions' => ['allowClear' => true],
                    ],
                    'filterInputOptions' => ['placeholder' => '选择状态'],
                    'pageSummaryOptions' => ['class' => 'text-right text-warning'],
                ],
                [
                    'class' => '\kartik\grid\ActionColumn',
                    'deleteOptions' => ['label' => '<i class="glyphicon glyphicon-remove"></i>'],
                    'template' => '{view}',

                ],
            ],
            'toolbar' => [
                Yii::$app->request->get('id') !== null ? [
                    'content' => Html::button('<i class="glyphicon glyphicon-plus"></i>', ['type' => 'button', 'title' => '选中归为档案', 'class' => 'btn btn-success', 'id' => 'addpig'])
                ] : '',
                '{export}',
                '{toggleData}',
            ],
            'bordered' => true,
            'condensed' => true,
        ]);
    } catch (Exception $e) {
    }
    ?>

</div>
</div>
</div>
</section>
</section>





